<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 用户详情
 *
 * @author      Eden
 * @datetime    2016/12/3 14:49
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Detail extends ADM_Controller
{
    const PAGE_SIZE = 20;

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $id = $this->input->get('id');

        if (empty($id) || $this->input->method(true) !== 'GET') {
            show_404() && exit;
        }

        // 获取交易记录
        // 获取个人信息
        // 获取收货地址
        $this->load->model('admin/user/User_Model', 'user');
        $this->load->view('admin/user/detail.html', array(
            'user' => $this->user->getUserInfo($id),
            'address' => $this->user->getAddressInfo($id)
        ));
    }

    /**
     * 获取微信列表
     */
    public function getWxLists()
    {
        $send['state'] = false;
        do {

            $userId = $this->input->get('userId');
            $page = $this->input->get('page');
            $level = $this->input->get('level');
            $level = empty($level) ? 1 : $level;

            if (empty($userId) || empty($level)) {
                $send['error'] = '参数错误';
                break;
            }

            $page = !empty($page) && intval($page) > 0 ? $page : 1;
            $offset = ($page - 1) * self::PAGE_SIZE;

            $this->load->model('admin/wx/SocialRelationShip_Model', 'relation');
            $fans = $this->relation->getFansListsByUserId($userId, $level, $offset, self::PAGE_SIZE);

            $getCounter = 'getFansCountByUserId_Level_' . $level;
            $total = $this->relation->$getCounter($userId);
            $data = [
                'lists' => !empty($fans) ? $fans : [],
                'total' => intval($total)
            ];

            $send['state'] = true;
            $send['data'] = $data;

        } while(false);

        $this->output->send($send);
    }

    /**
     * 帖子列表
     */
    public function getBbsLists()
    {
        $send['state'] = false;
        do {

            $userId = $this->input->get('userId');
            if (empty($userId)) {
                $send['error'] = '参数错误';
                break;
            }
            $page = $this->input->get('page');
            $page = !empty($page) && intval($page) > 0 ? $page : 1;
            $offset = ($page - 1) * self::PAGE_SIZE;

            $this->load->model('admin/bbs/Index_Model', 'bbs');
            $order = $this->bbs->getListsByUserId($userId, 'all', $offset, self::PAGE_SIZE);

            $send['state'] = true;
            $send['data'] = $order;
        } while(false);

        $this->output->send($send);
    }

    /**
     * 个人订单
     */
    public function getOrderLists()
    {
        $send['state'] = false;
        do {

            $userId = $this->input->get('userId');
            if (empty($userId)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('admin/order/Order_Model', 'order');
            $order = $this->order->getOrdersByUserId($userId);
            $send['state'] = true;
            $send['data'] = $order;
        } while(false);

        $this->output->send($send);
    }

    /**
     * 用户信息
     */
    public function getUserInfo()
    {
        $send['state'] = false;
        do {

            $userId = $this->input->get('userId');
            $this->load->model('admin/user/User_Model', 'user');
            $baseInfo = $this->user->getUserInfo($userId);
            $send['state'] = true;
            $send['data'] = $baseInfo;

        } while(false);
        $this->output->send($send);
    }

    /**
     * 获取列表
     */
    public function getAddress()
    {
        $send['state'] = false;
        do {

            $id = $this->input->get('userId');
            if (empty($id)) {
                $send['error'] = '参数错误';
                break;
            }
            $this->load->model('admin/user/User_Model', 'user');
            $addr = $this->user->getAddressInfo($id);
            $send['state'] = true;
            $send['data'] = $addr;

        }while(false);
        $this->output->send($send);
    }

    /**
     * 收藏商品
     */
    public function getCollect()
    {
        $send['state'] = false;
        do {

            $userId = $this->input->get('userId');
            if (empty($userId)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('common/Collect_Model', 'collect');
            $res = $this->collect->getCollectGoods($userId);
            $send['state'] = true;
            $send['data'] = $res;

        } while(false);
        $this->output->send($send);
    }

    /**
     * 取现记录
     */
    public function cashout()
    {
        $send['state'] = false;
        do {

            $userid = $this->input->get('userid');
            if (empty($userid)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('common/Cash_Model', 'cash');
            $lists = $this->cash->getCashoutApplyList($userid, null);

            $send['state'] = true;
            $send['data'] = $lists;

        } while(false);
        $this->output->send($send);
    }

    /**
     * 充值
     */
    public function cashin()
    {
        $send['state'] = false;
        do {

            $userid = $this->input->get('userid');
            if (empty($userid)) {
                $send['error'] = '参数错误';
                break;
            }

            $this->load->model('common/Cash_Model', 'cash');
            $res = $this->cash->getCashinDetail($userid);

            $send['state'] = true;
            $send['data'] = $res;

        } while(false);
        $this->output->send($send);
    }

}
